/*
 * @LastEditors: 七七
 * @Date: 2023-01-06 10:24:46
 */
import React, { Component } from "react";
import axios from "axios";

export default class Search extends Component {
  search = () => {
    //获取用户输入(连续解构赋值:{keyWordElement:{value}}+重命名{value:keyWord})
    const {
      keyWordElement: { value: keyWord },
    } = this;
    //发送请求前通知App更新状态
    this.props.updateAppState({ isFirst: false, isLoding: true });
    //发送网络请求
    axios.get(`/api1/search/users?q=${keyWord}`).then(
      (response) => {
        this.props.updateAppState({
          isLoding: false,
          users: response.data.items,
        });
      },
      (error) => {
        // err要传error.message,不能直接传error，否则路径错误页面不显示内容
        this.props.updateAppState({ isLoding: false, err: error.message });
      }
    );
  };

  render() {
    return (
      <div>
        <section className="jumbotron">
          <h3 className="jumbotron-heading">搜索Github用户</h3>
          <div>
            <input
              ref={(c) => (this.keyWordElement = c)}
              type="text"
              placeholder="点击关键词搜索"
            />
            &nbsp;
            <button onClick={this.search}>搜索</button>
          </div>
        </section>
      </div>
    );
  }
}
